import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';

export interface WxAiChatMessagesAttributes {
  id: string;
  userId?: string;
  content?: string;
  functionName?: string;
  functionArguments?: object;
  model?: string;
  role: string;
  tokens?: number;
  createdTime: Date;
}

export type WxAiChatMessagesPk = 'id';
export type WxAiChatMessagesId = WxAiChatMessages[WxAiChatMessagesPk];
export type WxAiChatMessagesOptionalAttributes =
  | 'userId'
  | 'content'
  | 'functionName'
  | 'functionArguments'
  | 'model'
  | 'tokens'
  | 'createdTime';
export type WxAiChatMessagesCreationAttributes = Optional<
  WxAiChatMessagesAttributes,
  WxAiChatMessagesOptionalAttributes
>;

export class WxAiChatMessages
  extends Model<WxAiChatMessagesAttributes, WxAiChatMessagesCreationAttributes>
  implements WxAiChatMessagesAttributes
{
  id!: string;
  userId?: string;
  content?: string;
  functionName?: string;
  functionArguments?: object;
  model?: string;
  role!: string;
  tokens?: number;
  createdTime!: Date;

  static initModel(sequelize: Sequelize.Sequelize): typeof WxAiChatMessages {
    return WxAiChatMessages.init(
      {
        id: {
          type: DataTypes.CHAR(36),
          allowNull: false,
          primaryKey: true,
          comment: 'UUID 消息ID',
        },
        userId: {
          type: DataTypes.CHAR(36),
          allowNull: true,
          field: 'user_id',
        },
        content: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        functionName: {
          type: DataTypes.STRING(100),
          allowNull: true,
          comment: '函数调用名称（如有）',
          field: 'function_name',
        },
        functionArguments: {
          type: DataTypes.JSON,
          allowNull: true,
          comment: '函数调用参数（如有）',
          field: 'function_arguments',
        },
        model: {
          type: DataTypes.STRING(100),
          allowNull: true,
          comment: '生成该回复的模型',
        },
        role: {
          type: DataTypes.STRING(10),
          allowNull: false,
          comment: '角色',
        },
        tokens: {
          type: DataTypes.INTEGER,
          allowNull: true,
          comment: '该消息的token数',
        },
        createdTime: {
          type: DataTypes.DATE,
          allowNull: false,
          defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
          comment: '新建时间',
          field: 'created_time',
        },
      },
      {
        sequelize,
        tableName: 'wx_ai_chat_messages',
        timestamps: false,
        indexes: [
          {
            name: 'PRIMARY',
            unique: true,
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
        ],
      },
    );
  }
}
